<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function arr(matrix) {
      // 用ma[i][j]表示以该点为右下角正方形的最大边长
      // 且ma[i][j]=max(ma[i-1][j], ma[i][j-1], ma[i-1][j-1]) + 1;
      const long = matrix.length;
      if (long < 1) {
        return 0;
      }
      const width = matrix[0].length;
      let max = 0;
      for (let i = 0; i < long; i++) {
        for (let j = 0; j < width; j++) {
          if (matrix[i][j] === '1') {
            matrix[i][j] = i !== 0 && j !== 0 ? Math.min(matrix[i - 1][j], matrix[i][j - 1], matrix[i - 1][j - 1]) + 1 : 1;
            max = Math.max(max, matrix[i][j]);
          } else {
            matrix[i][j] = 0;
          }
        }
      }
      return max * max;
    };
    console.log(arr([["1", "0", "1", "0", "0"], ["1", "0", "1", "1", "1"], ["1", "1", "1", "1", "1"], ["1", "0", "0", "1", "0"]]));
  </script>
</body>

</html>